import 'dart:convert';
import 'dart:developer';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';

import '../utils/constant.dart';
import 'network_url.dart';
import 'package:http/http.dart' as http;

class DioManager {
  static  DioManager _instance =DioManager();
  static DioManager getInstance() {
    _instance ??= DioManager();
    return _instance;
  }


  getRequest2(String url,  params, Function successCallBack,
      Function errorCallBack) async {
    print(url);
    _requestHttp(url, successCallBack, 'get', "2", params, errorCallBack,false);
  }


  //get请求
  getRequest(String url,  params, Function successCallBack,
      Function errorCallBack) async {
    print(url);
    _requestHttp(url, successCallBack, 'get', "1", params, errorCallBack,false);
  }


  //post请求
  post(String url, params, Function successCallBack,
      Function errorCallBack,{ needErrorMsg=true,}) async {
    _requestHttp(url, successCallBack, "post", "1", params, errorCallBack,false,needErrorMsg);
  }
  //post请求
  post2(String url, params, Function successCallBack,
      Function errorCallBack,{ needErrorMsg=true,}) async {
    _requestHttp(url, successCallBack, "post", "2", params, errorCallBack,false,needErrorMsg);
  }
  postJson(String url, params, Function successCallBack,
      Function errorCallBack) async {

    _requestHttp(url, successCallBack, "post", "json", params, errorCallBack,false);
  }

  postJson2(String url, params, Function successCallBack,
      Function errorCallBack) async {
    _requestHttp(url, successCallBack, "post", "json",params, errorCallBack,true);
  }

  _requestHttp(String url, Function successCallBack,
      [String? method, String? type,  params, Function? errorCallBack,notShowLoading,needErrorMsg]) async {


    // if (type != "2" && !notShowLoading) {
    //   BotToast.showLoading();
    // }
    var response;
    if(type=='2'){
      Map map={'appKey':Constant.appKey};
      // if(params!=null){
      //   map = Map.from(map)..addAll(params);
      // }
      Uri urls = Uri.https(Constant.baseUrlIm,url);
      if(method=='get'){
        response = await http.get(urls, headers: <String, String>{
          "appKey":Constant.appKey
        });
      }else{
        response = await http.post(urls, headers: <String, String>{
          'Content-Type': 'application/x-www-form-urlencoded',
          "Accept": "application/json",
        },
            body: map
        );
      }

    }else{
      Map map={'app_key':'dsjgbndfgtfbdaxcdazz'};
      if(params!=null){
        map = Map.from(map)..addAll(params);
      }
      Uri urls = Uri.http(Constant.baseUrl,url,{});
      response = await http.post(urls, headers: <String, String>{
        'Content-Type': 'application/x-www-form-urlencoded',
        "Accept": "application/json",
      },
          body: map
      );
    }


//    if (Constant.ISDEBUG) {
    print('请求url: $url');
    if (params != null && type != 'json') {
      print('请求参数: ');

      params.forEach((key, value) {
        print('${key}:${value}');
      });

    } else {
      print('请求json参数: ');
      print(json.encode(params));
    }
    if (response != null) {
      print('返回json内容:');
      print(response.body);
    }
    //返回结果
    BotToast.closeAllLoading();
    if (response.body .isNotEmpty) {
      if(url.contains(RegExp('getAddRecharge'))){
        successCallBack(response.body);
      }else{
        if (response?.statusCode == 200) {
          var instanceMap = Map();
          instanceMap['code'] = 200;
          successCallBack(response.body);
        } else {
          if(needErrorMsg){
            _error(errorCallBack!, 'error');
          }else{
            successCallBack(response.body);
          }

        }
      }

    } else {
        successCallBack(response.toString());

    }
  }

  _error(Function errorCallBack, String error) {
    BotToast.closeAllLoading();
    if (errorCallBack != null) {
      errorCallBack(error);
    }
  }

}

